#!/bin/sh

#desc:Add IP Camera
#package:odvideo
#type:local

# Copyright(c) 2011 OpenDomo Services SL. Licensed under GPL v3 or later
# Isidro Catalan <skarvin@gmail.com>

# Common video module vars
if ! test -f "/etc/opendomo/videoConfVars.conf"; then
   cp /etc/opendomo/videoConfVars.conf.orig /etc/opendomo/videoConfVars.conf
fi
. "/etc/opendomo/videoConfVars.conf"

# Lib config
. "$LIB_CONFIG"
CONF_FILE="$COMMON_CONF_PATH"

# if it doesn't exist, it is created
if ! test -f "$CONF_FILE"; then
   touch "$CONF_FILE"
fi

# Lib Net
. "$LIB_NET"
# ----------------------------------------------------------------------------
# desc: Add IP Camera
# author: Isidro Catalan <skarvin@gmail.com>, http://www.opendomo.org
# date: September 2009
#
# CHANGES:
# ----------------------------------------------------------------------------

if test -x /usr/bin/record; then

	# First we check if we've got target dir	
	path=`getValue "TARGET_DIR"`
	if [ ! -d "$path" ]; then
		# If it does not exist, we try to create it
		mkdir -p "$path" 2>/dev/null
	fi
	
	if [ ! -d "$path" ]; then
		echo "#INF: You need to specify a folder to store your captures"
		. /usr/local/opendomo/services/config/video/addStorage.sh 2> /dev/null	
	fi
	
	if ([ "$name_cam" != "" ] && [ "$netcam_url" = "" ]) || ([ "$name_cam" = "" ] && 	[ "$netcam_url" != "" ]); then
			echo "#ERR You need to speecify a camera name and the camera's direction"
	fi

fi

if [ "$name_cam" != "" ] && [ "$netcam_url" != "" ]; then
	if test -x /usr/bin/record; then
		# First we check if we've got target dir	
		path=`getValue "TARGET_DIR"`
		if [ ! -d "$path" ]; then
			# If it does not exist, we try to create it
			mkdir -p "$path" 2>/dev/null
		fi

		if [ ! -d "$path" ]; then
			echo "#ERR You need a valid target directory in order to set a new 	IP Camera"
			exit 1
		fi
	fi
	if [ "$netcam_user" != "" ] ||  [ "$netcam_pass" != "" ]; then
		#echo "#INF: Skipping URL validation, http authentication enabled"
		echo
	else
		# Check whether the URL is valid
		res=`checkURL "$netcam_url"`
		if [ "$res" != "0" ]; then
			echo "#ERR: The Url provided seems not to be working or the http authentication is enabled, please check it and try it again"
			exit 1
		fi
	fi   

	no_spaces_name=`echo $name_cam | tr " +áéíóúÑñÁÉÍÓÚÀÈÌÒÙ" "__aeiounNnAEIOUAEIOU"`
	
	# Check for repeated names
	for conf_file in `ls $VIDEO_CONF_PATH/*.conf 2>/dev/null`; do
		# Obtenemos el nombre y la url de streaming de la camara en cuestion
		cam_name=`basename "$conf_file" | cut -d"." -f1`
		if [ "$cam_name" = "$no_spaces_name" ]; then
			echo "#ERR: There is a camera with the same name already."
			exit 1
		fi
	done
	
	# adding config files
	CONF_FILE="$VIDEO_CONF_PATH/$no_spaces_name.conf"
	touch "$CONF_FILE"
	setValue "NAME" "$name_cam"	
	setValue "URL" "$netcam_url"
	if [ "$netcam_user" != "" ] || [ "$netcam_pass" != "" ]; then
		setValue "AUTH" "$netcam_user:$netcam_pass"	
	else
		setValue "AUTH" ""	
	fi

	setValue "ZONE" "$cam_zone" 	
	setValue "NIGHT_VISION" "$night_vision" 
	setValue "SENSITIVITY" "$sensitivity"
		
	if [ "$?" = "0" ]; then
		echo "#INF: IP camera added succesfully"
	else
		echo "#ERR: IP camera adding failed, check log for more information"
	fi
	/usr/local/opendomo/manageCameras.sh
	exit 0
fi

# Zones loaded
cd $ZONEDIR
for z in *; do
	if test "$z" != "*"; then
		if test -z $TAGS; then
			TAGS="$z"
		else
			TAGS="$TAGS,$z"
		fi
	fi
done
################ FORMULARIOS #####################
# Formulario para agregar camara IP
echo "#> Adding a new IP Camera"
echo "form:addIPCam.sh"
echo "	name_cam	Name	text	$F1"
echo "	cam_zone	Zone	list[$TAGS]	$ZONE"
echo "	netcam_url	URL Streaming	text	$F2"
echo "	netcam_user	HTTP user authentication	text	$F3"
echo "	netcam_pass	HTTP password authentication	text	$F4"
echo "	preview	Preview	image $F2"
	if test -x /usr/bin/record; then
echo -n "
	night_vision	Covered camera detection	list[1:Yes,0:No]	$F5
	sensitivity	Sensitivity	list[100:Very high,300:High,500:Normal,700:Low, 900:Very low]	$F6
"
fi
echo  "actions:"
echo "	manageCameras.sh	Back to list"
echo  "	addIPCam.sh	Apply"
echo ""


